ENSAE Pierre NDIAYE de Dakar ISE1-Cycle long 2024-2025
COURS DE Statistique exploratoire spaciale avec M.Aboubacar
HEMA
Travaux Pratiques N°6
Groupe : Logiciel R — Composé de Jeanne De La Flèche ONANENA AMANA,
Khadidiatou COULIBALY, Tamsir NDONG, Samba DIENG
===================== CONSIGNE =================
- Importation et visualisation
- Calcul du nombre d’attaques par admin
- Création de rasters autour des attaques
- Création de rasters pour chaque année et visualisation de
nombre d’attaques par année
Importation des packages et work directory setting
suppressMessages({
# Charger les bibliothèques nécessaires
library(ggplot2)
library(dplyr)
library(leaflet)
library(sf)
library(raster)
library(terra)
library(leaflet.extras)
library(viridis)
library(kableExtra)
})
## Warning: le package 'leaflet.extras' a été compilé avec la version R 4.4.2
## Setting the work directory
knitr::opts_knit$set(root.dir =
"C:/Users/DELL/Documents/ISEP3_2025/Stats_spatiale/Statistique-Exploratoire-Spatiale/TP6")
knitr::opts_chunk$set(fig.align = "center", fig.width = 6)
Importation et visualisation
# Charger les données depuis un fichier CSV
data <- read.csv("data/Points_data.csv")
shp <- st_read("Groupe - R/shapefiles/mli_admbnda_adm1_1m_gov_20211220.shp", quiet= TRUE)
## 'data.frame': 87225 obs. of 31 variables:
## $ event_id_cnty : chr "MLI33140" "BFO12659" "BFO12661" "BFO12665" ...
## $ event_date : chr "04 October 2024" "04 October 2024" "04 October 2024" "04 October 2024" ...
## $ year : int 2024 2024 2024 2024 2024 2024 2024 2024 2024 2024 ...
## $ time_precision : int 1 1 1 1 1 1 1 1 2 1 ...
## $ disorder_type : chr "Political violence" "Political violence" "Political violence" "Political violence" ...
## $ event_type : chr "Explosions/Remote violence" "Explosions/Remote violence" "Battles" "Battles" ...
## $ sub_event_type : chr "Air/drone strike" "Air/drone strike" "Armed clash" "Armed clash" ...
## $ actor1 : chr "Military Forces of Mali (2021-)" "Military Forces of Burkina Faso (2022-)" "JNIM: Group for Support of Islam and Muslims" "JNIM: Group for Support of Islam and Muslims" ...
## $ assoc_actor_1 : chr "" "" "" "" ...
## $ inter1 : chr "State forces" "State forces" "Rebel group" "Rebel group" ...
## $ actor2 : chr "Civilians (Niger)" "JNIM: Group for Support of Islam and Muslims" "Military Forces of Burkina Faso (2022-)" "VDP: Volunteer for Defense of Homeland" ...
## $ assoc_actor_2 : chr "Miners (Niger)" "" "VDP: Volunteer for Defense of Homeland" "" ...
## $ inter2 : chr "Civilians" "Rebel group" "State forces" "Identity militia" ...
## $ interaction : chr "State forces-Civilians" "State forces-Rebel group" "State forces-Rebel group" "Rebel group-Identity militia" ...
## $ civilian_targeting: chr "Civilian targeting" "" "" "" ...
## $ iso : int 466 854 854 854 854 854 288 466 566 566 ...
## $ region : chr "Western Africa" "Western Africa" "Western Africa" "Western Africa" ...
## $ country : chr "Mali" "Burkina Faso" "Burkina Faso" "Burkina Faso" ...
## $ admin1 : chr "Kidal" "Cascades" "Sahel" "Nord" ...
## $ admin2 : chr "Abeibara" "Comoe" "Soum" "Yatenga" ...
## $ admin3 : chr "Tinzawatene" "Ouo" "Djibo" "Ouahigouya" ...
## $ location : chr "Zakak" "Dida Forest" "Djibo" "Aorema" ...
## $ latitude : num 19.6 10 14.1 13.7 12.7 ...
## $ longitude : num 2.891 -4.029 -1.642 -2.334 -0.131 ...
## $ geo_precision : int 1 2 2 1 2 1 1 1 2 1 ...
## $ source : chr "FAMAMali; Twitter; Undisclosed Source" "Undisclosed Source" "Al Zallaqa; Whatsapp" "Whatsapp" ...
## $ source_scale : chr "Local partner-Other" "Local partner-Other" "New media" "New media" ...
## $ notes : chr "On 4 October 2024, the Malian air force carried out an airstrike against a convoy in the village of Zakak (Abei"| __truncated__ "On 4 October 2024, the Burkinabe force carried airstrikes against JNIM militants in the Dida Forest (Ouo, Comoe"| __truncated__ "On 4 October 2024, JNIM militants carried out several attacks against positions of Burkinabe force and voluntee"| __truncated__ "On 4 October 2024, JNIM militants attacked a position of volunteer fighters (VDP) in the village of Aorema (Oua"| __truncated__ ...
## $ fatalities : int 7 5 0 0 0 0 0 3 0 2 ...
## $ tags : chr "" "" "" "" ...
## $ timestamp : int 1728335020 1728358478 1728358478 1728358478 1728358478 1728358478 1728358486 1728358502 1728358504 1728358504 ...
# Voyons les noms des variables
colnames(data)
## [1] "event_id_cnty" "event_date" "year"
## [4] "time_precision" "disorder_type" "event_type"
## [7] "sub_event_type" "actor1" "assoc_actor_1"
## [10] "inter1" "actor2" "assoc_actor_2"
## [13] "inter2" "interaction" "civilian_targeting"
## [16] "iso" "region" "country"
## [19] "admin1" "admin2" "admin3"
## [22] "location" "latitude" "longitude"
## [25] "geo_precision" "source" "source_scale"
## [28] "notes" "fatalities" "tags"
## [31] "timestamp"
# VOYONS LES PAYS
unique(data$country)
## [1] "Mali" "Burkina Faso" "Ghana" "Nigeria"
## [5] "Benin" "Guinea" "Senegal" "Ivory Coast"
## [9] "Guinea-Bissau" "Mauritania" "Niger" "Togo"
## [13] "Liberia" "Cape Verde" "Gambia" "Sierra Leone"
# Convertir les données en un objet spatial sf
data_spatial <- sf::st_as_sf(data, coords = c("longitude", "latitude"), crs = st_crs(shp))
## Voir cela sur leaflet...
# Visualisation pour les différents pays
ggplot(data_spatial) +
geom_sf(fill = NA, color = "blue", size = 0.5) +
aes(colour = country) +
geom_sf(size = 1.2) +
scale_fill_hue(direction = 1) +
scale_color_hue(direction = 1)

## Palette de couleurs
country_palette <- country_palette <- colorFactor(viridis(length(unique(data$country)),
option = "turbo"), domain = data$country)
# Créer une carte interactive
leaflet() %>%
addTiles() %>% # Couche de base (OpenStreetMap)
# Ajouter les limites (administration de niveau 0 - national)
addPolygons(data = shp, color = "brown", weight = 2, opacity = 1, fillOpacity = 0.5,
popup = ~ADM1_FR) %>% # Afficher l'information dans une popup
# Ajouter les points d'événements (assurez-vous que AOI_event est un objet sf avec un CRS défini)
addCircleMarkers(data = data, weight = 0.1, opacity = 2, fillOpacity = 1.4,
radius = 1.5, # Adjust circle size
color = ~country_palette(country)) %>%
addLegend("bottomright", pal = country_palette, values = data$country,
title = "Evenements par pays", opacity = 1) %>%
addResetMapButton()%>% # Recentrer la carte
addFullscreenControl() #ajout du basculement en mode plein écran
## Assuming "longitude" and "latitude" are longitude and latitude, respectively
On choisit notre zone d’intérêt
# On selectionne notre pays (Area of interest)
AOI = "Mali"
# Filtrer pour le Sénégal
AOI_event <- data_spatial %>%
filter(country == AOI)
# Visualisation des événements au Sénégal
ggplot(AOI_event) +
aes(fill = event_type, colour = event_type) +
geom_sf(size = 1.2) +
scale_fill_hue(direction = 1) +
theme_minimal()

## Palette de couleurs
event_palette <- colorFactor(palette = "Set2", domain = AOI_event$event_type)
# Créer une carte interactive
leaflet() %>%
addTiles() %>% # Couche de base (OpenStreetMap)
# Ajouter les limites (administration de niveau 0 - national)
addPolygons(data = shp, color = "brown", weight = 2, opacity = 1, fillOpacity = 0.5,
popup = ~ADM1_FR) %>% # Afficher l'information dans une popup
# Ajouter les points d'événements (assurez-vous que AOI_event est un objet sf avec un CRS défini)
addCircleMarkers(data = AOI_event, weight = 0.1, opacity = 2, fillOpacity = 1.4,
radius = 2, # Adjust circle size
color = ~event_palette(event_type)) %>%
addLegend("bottomright", pal = event_palette, values = AOI_event$event_type,
title = "Event Type", opacity = 1) %>%
addResetMapButton()%>% # Recentrer la carte
addFullscreenControl() #ajout du basculement en mode plein écran
## 2. Calcul du nombre d’évènements par admin (0-3)
# On joint les deux
points_ml<- st_join(data_spatial, shp, join = st_intersects)
points_ml %>% data.frame() %>% tail(5) %>% kable()
| 87221 |
SIE2 |
01 January 1997 |
1997 |
3 |
Political violence |
Battles |
Government regains territory |
Military Forces of Sierra Leone (1996-1997) |
|
State forces |
RUF: Revolutionary United Front |
|
Rebel group |
State forces-Rebel group |
|
694 |
Western Africa |
Sierra Leone |
Eastern |
Kono |
Gbane |
Mandu |
2 |
No Peace Without Justice; SL-LED |
Local partner-New media |
Around 1 January 1997 (month of), Military Forces of
Sierra Leone (1996-1997) and RUF: Revolutionary United Front engaged in
battles in Mandu (Eastern, Kono). No fatalities. |
0 |
|
1670286851 |
NA |
NA |
NA |
NA |
NA |
NA |
NA |
NA |
NA |
NA |
NA |
NA |
POINT (-10.9332 8.4642) |
| 87222 |
SIE3 |
01 January 1997 |
1997 |
3 |
Political violence |
Battles |
Armed clash |
Kamajor Militia |
|
Political militia |
RUF: Revolutionary United Front |
|
Rebel group |
Rebel group-Political militia |
|
694 |
Western Africa |
Sierra Leone |
Southern |
Bo |
Selenga |
Selenga |
2 |
No Peace Without Justice; SL-LED |
Local partner-New media |
Around 1 January 1997 (month of), Kamajor Militia and
RUF: Revolutionary United Front engaged in battles in Selenga (Southern,
Bo). No fatalities. |
0 |
|
1670286851 |
NA |
NA |
NA |
NA |
NA |
NA |
NA |
NA |
NA |
NA |
NA |
NA |
POINT (-11.7047 8.1221) |
| 87223 |
SIE6 |
01 January 1997 |
1997 |
3 |
Political violence |
Violence against civilians |
Attack |
Military Forces of Sierra Leone (1996-1997) |
|
State forces |
Civilians (Sierra Leone) |
|
Civilians |
State forces-Civilians |
Civilian targeting |
694 |
Western Africa |
Sierra Leone |
Southern |
Bonthe |
Imperi |
York Island |
2 |
No Peace Without Justice; SL-LED |
Local partner-New media |
Around 1 January 1997 (month of), Military Forces of
Sierra Leone (1996-1997) engaged in violence against civilians in York
Island (Southern, Bonthe). No fatalities. |
0 |
|
1670286851 |
NA |
NA |
NA |
NA |
NA |
NA |
NA |
NA |
NA |
NA |
NA |
NA |
POINT (-12.4694 7.5317) |
| 87224 |
SIE7 |
01 January 1997 |
1997 |
3 |
Political violence |
Battles |
Armed clash |
Kamajor Militia |
|
Political militia |
Military Forces of Sierra Leone (1996-1997) |
|
State forces |
State forces-Political militia |
|
694 |
Western Africa |
Sierra Leone |
Southern |
Moyamba |
Fakunya |
Fakunya |
2 |
SL-LED; No Peace Without Justice |
Local partner-New media |
Around 1 January 1997 (month of), Kamajor Militia and
Military Forces of Sierra Leone (1996-1997) engaged in battles in
Fakunya (Southern, Moyamba). No fatalities. |
0 |
|
1670286851 |
NA |
NA |
NA |
NA |
NA |
NA |
NA |
NA |
NA |
NA |
NA |
NA |
POINT (-12.338 8.231) |
| 87225 |
SIE8 |
01 January 1997 |
1997 |
3 |
Strategic developments |
Strategic developments |
Headquarters or base established |
Military Forces of Sierra Leone (1996-1997) |
|
State forces |
|
|
|
State forces only |
|
694 |
Western Africa |
Sierra Leone |
Southern |
Moyamba |
Kori |
Taiama |
2 |
No Peace Without Justice; SL-LED |
Local partner-New media |
base |
0 |
|
1678830926 |
NA |
NA |
NA |
NA |
NA |
NA |
NA |
NA |
NA |
NA |
NA |
NA |
POINT (-12.06 8.2013) |
# On garde les points du Mali
points_ml <- points_ml %>% filter(!is.na(ADM1_PCODE))
points_ml %>% data.frame() %>% dim()
## [1] 11547 42
## Différence de 6 points...
dim(AOI_event)
## [1] 11541 30
Nombre d’évènements par région…méthode 1
Dans ce cas, on a utilisé l’objet point_counts pour calculer le
nombre d’attaques par admin. Si cet objet avait été créé avec le
shapefile d’une autre division administrative, il permettrait de faire
la même chose pour cette division.
Une idée serait de créer ici une fonction.
# Compter le nombre de points par région
point_counts <- points_ml %>%
group_by(ADM1_FR) %>% # groupper par régions
summarise(Nombre_attaques = n())
point_counts %>%
st_drop_geometry() %>%
data.frame() %>% kable()
| Bamako |
446 |
| Gao |
1956 |
| Kayes |
265 |
| Kidal |
935 |
| Koulikoro |
429 |
| Menaka |
720 |
| Mopti |
3852 |
| Sikasso |
308 |
| Ségou |
1380 |
| Tombouctou |
1256 |
t1 <-table(AOI_event$admin1, AOI_event$event_type)
kable(t1)
| Bamako |
31 |
6 |
245 |
65 |
58 |
41 |
| Gao |
476 |
251 |
78 |
30 |
444 |
679 |
| Kayes |
79 |
4 |
75 |
28 |
28 |
51 |
| Kidal |
237 |
368 |
44 |
27 |
135 |
124 |
| Koulikoro |
132 |
54 |
40 |
26 |
72 |
103 |
| Menaka |
211 |
93 |
13 |
7 |
137 |
246 |
| Mopti |
1095 |
521 |
105 |
39 |
599 |
1498 |
| Segou |
410 |
199 |
35 |
14 |
231 |
494 |
| Sikasso |
79 |
25 |
44 |
44 |
58 |
58 |
| Tombouctou |
300 |
216 |
59 |
44 |
225 |
411 |
t2 <- table(AOI_event$admin2) %>% data.frame()
colnames(t2) <- c("division", "nb_events")
head(t2,10) %>% kable()
| Abeibara |
117 |
| Anderamboukane |
109 |
| Ansongo |
824 |
| Bafoulabe |
32 |
| Bamako |
446 |
| Banamba |
52 |
| Bandiagara |
763 |
| Bankass |
481 |
| Baraoueli |
12 |
| Bla |
14 |
t3 <- table(AOI_event$admin3) %>% data.frame()
colnames(t3) <- c("division", "nb_events")
head(t3, 10) %>% kable()
| Abeibara |
49 |
| Adjelhoc |
136 |
| Alafia |
44 |
| Anchawadi |
96 |
| Anderamboukane |
110 |
| Anefif |
57 |
| Ansongo |
164 |
| Arham |
1 |
| Bafoulabe |
11 |
| Baguindabougou |
1 |
3. Création de rasters autour des attaques
On crée des fonctions d’abord
# Reprojeter pour obtenir des unités en mètres
Create_raster <- function(datafile, filename ="Rasterisation.tif") {
# Reprojeter pour obtenir des unités en mètres
AOI_prj <- st_transform(datafile, crs = 32629) # EPSG 32629 pour UTM zone 29N -- pour le Mali
# Définir l'étendue (extent) et la résolution en mètres
ext <- raster::extent(sf::st_bbox(AOI_prj)) #extent
res <- 5000 # Résolution de 5 km
rast_crs <- CRS("+proj=utm +zone=29 +datum=WGS84 +units=m +no_defs")
raster_template <- raster::raster(ext=ext, resolution=res, crs=rast_crs)
# Evènements...
Raster <- raster::rasterize(AOI_prj,raster_template,field=1, fun= sum)
#Save with GTIFF format
raster::writeRaster(Raster, filename = filename, format = "GTiff", overwrite = TRUE)
return(Raster)
}
AOI_Raster <- Create_raster(AOI_event, "Rasterisation_general.tif")
## Fonction pour affciher une carte
Create_map <- function(raster){
## Palette de couleurs
pal <- colorNumeric(palette = viridis(1000, option = "viridis"),
domain = raster::values(raster),
na.color = "transparent")
# Créer une carte interactive
leaflet() %>%
addTiles() %>% # Couche de base (OpenStreetMap)
addPolygons(data = shp, color = "brown", weight = 2, opacity = 0.2, fillOpacity = 0.1,
popup = ~ADM1_FR) %>%
addRasterImage(raster, opacity = 2,colors= pal) %>%
addLegend(pal = pal, values = raster::values(raster),
title = "Nombre d'événement") %>%
addResetMapButton() %>% # Recentrer la carte
addFullscreenControl() #ajout du basculement en mode plein écran
}
## Warning in colors(.): Some values were outside the color scale and will be
## treated as NA
summary(raster::values(AOI_Raster))
## Min. 1st Qu. Median Mean 3rd Qu. Max. NA's
## 1.0 1.0 2.0 5.8 4.5 445.0 94959
4. Création de raster pour chaque année et visualistion du nombre
d’attaques par année
for( i in unique(data$year)){
assign(paste0("data_", i), data[data$year == i,])
assign(paste0("AOI_Raster_", i), Create_raster(AOI_event, paste0("Rasterisation_", i, ".tif")))
}
## event_id_cnty event_date year time_precision disorder_type
## 42787 CDI2482 31 December 2020 2020 1 Political violence
## 42788 NIG19389 31 December 2020 2020 1 Demonstrations
## event_type sub_event_type
## 42787 Violence against civilians Attack
## 42788 Riots Violent demonstration
## actor1 assoc_actor_1 inter1
## 42787 Unidentified Armed Group (Ivory Coast) Political militia
## 42788 Rioters (Nigeria) Rioters
## actor2 assoc_actor_2 inter2
## 42787 Civilians (Ivory Coast) Government of the Ivory Coast (2011-) Civilians
## 42788
## interaction civilian_targeting iso region
## 42787 Political militia-Civilians Civilian targeting 384 Western Africa
## 42788 Rioters only 566 Western Africa
## country admin1 admin2 admin3 location latitude
## 42787 Ivory Coast Comoe Indenie-Djuablin Abengourou Abengourou 6.7297
## 42788 Nigeria Ogun Egbado South Oke Odan 6.7000
## longitude geo_precision source source_scale
## 42787 -3.4964 2 Afrique sur 7; Abidjan TV National-International
## 42788 2.9000 1 Daily Post (Nigeria) National
## notes
## 42787 On 31 December 2020, unidentified individuals opened fire on the road toll station of Thomasset town, in the Eastern Ivory Coast, assumedly around Abengourou town (Indenie-Djuablin, Comoe). They destroyed properties. No fatality and/or casualty reported.
## 42788 On 31 December 2020, youth demonstrators set up bonfires and blocked the International Road in Oke Odan (Egbado South LGA, Ogun) over the killing of another youth by Nigeria Customs Service operatives.
## fatalities tags timestamp
## 42787 0 1610409617
## 42788 0 crowd size=no report 1610409633
Create_map(AOI_Raster_2020)
## Warning in colors(.): Some values were outside the color scale and will be
## treated as NA
Créons des tableaux contenant les données à visualiser
t <- data %>%
group_by(year, event_type) %>%
summarise(attacks_number =n())
## `summarise()` has grouped output by 'year'. You can override using the
## `.groups` argument.
t1 <- data %>%
group_by(year) %>%
summarise(attacks_number =n())
## # A tibble: 3 × 3
## # Groups: year [1]
## year event_type attacks_number
## <int> <chr> <int>
## 1 1997 Battles 357
## 2 1997 Explosions/Remote violence 24
## 3 1997 Protests 47
ggplot(t1, aes(y= attacks_number, x=year))+
geom_path(linewidth= 1.2, color="blue")+
geom_point(size=2, color="red")+
theme_minimal() +
labs(title = "Nombre d'attaques par année")

ggplot(t, aes(y= attacks_number, x=year, color = event_type, group=event_type))+
geom_path(linewidth= 1.2)+
geom_point(size=2)+
theme_minimal() +
labs(title = "Nombre d'attaques par année")
